<?php
/**
 * 会员前台管理中心、账号管理、收藏操作类
 */

defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('foreground');
pc_base::load_sys_class('format', '', 0);
pc_base::load_sys_class('form', '', 0);

class business extends foreground {

	private $business_db;
	
	function __construct() {
		parent::__construct();
		$this->http_user_agent = str_replace('7.0' ,'8.0',$_SERVER['HTTP_USER_AGENT']);
		$this->business_db = pc_base::load_model('business_model');
		
		
	}

	public function init() {
		$memberinfo = $this->memberinfo;
		//初始化phpsso
		$phpsso_api_url = $this->_init_phpsso();
		//获取头像数组
		$avatar = $this->client->ps_getavatar($this->memberinfo['phpssouid']);

		$grouplist = getcache('grouplist');
		print_r($grouplist);
		$memberinfo['groupname'] = $grouplist[$memberinfo[groupid]]['name'];

		include template('member', 'index');

	}
	/*处理设计师信息*/
	function business_info(){
		$member = $this->business_db->get_one(array('userid'=>$this->memberinfo['userid']));

		if(isset($_POST['dosubmit']))
		{
			$data = $_POST['info'];
			if(is_array($member)){
				$this->business_db->update($data, array('userid'=>$member['userid']));
			}
			else
			{
				$data['userid']= $this->memberinfo['userid'];
				$this->business_db->insert($data);
			}
			showmessage(L('operation_success'), HTTP_REFERER);
		}
		else
		{
			include template('member', 'business_info');
		}
	}
	/**
	 * 
	 * ajax取得空间类型
	 */
	function get_space_type(){
		$types = getcache('3366', 'linkage');
		$data = $types['data'];
		$id = $_POST['id'];
		$str = '<select name="work[space_type2]">';
		if($id!=0){
			if(is_array($data))
			{
				foreach ($data as $k=>$v){
				
					if($v['parentid']!= $id || $v['parentid']==0){
						unset($data[$k]);
					}
				}
				
				foreach ($data as $key=>$val){
					$str .= '<option value="'.$val['linkageid'].'">'.$val['name'].'</option>';
				}
				
				$str .= '</select>';
			}	
		}
		else
		{
			$str .= '<option vaule="">请选择</option>';	
		}
		$str .= '</select>';
		$data = array();
		echo $str;
	}
	/**
	 * 
	 * 证书管理
	 */
	function certificate(){
		if(isset($_POST['dosubmit']))
		{
			$data = $_POST['info'];
			$data['userid']= $this->memberinfo['userid'];
			$this->certificate_db->insert($data);
			showmessage(L('operation_success'), HTTP_REFERER);
			
		}
		include template('member', 'desginer_cert');
	}
/**
	 * 
	 * 留言咨询管理
	 */
	function message(){
		$this->messages_db = pc_base::load_model('messages_model');
		$table_pre = $this->messages_db->db_tablepre;
		$member_company_db = $table_pre.'member_company';
		$message_db = $table_pre.'messages';
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 15;
		$offset = intval($pagesize*($page-1));
		
	$sql = '
		SELECT * FROM `v9_messages` WHERE rid=0 and `replyid`=0 and userid ='.$this->memberinfo['userid'];
			//$sql = 'select a.* from '.$message_db.' as a left join 
			//'.$member_company_db.' as b on a.companyid=b.companyid where a.companyid = //'.intval($_GET['companyid']);
			$sql_count = 'SELECT count(*) as num FROM `v9_messages` WHERE rid=0 and `replyid`=0 and userid ='.$this->memberinfo['userid'];
		
		$sql .= " LIMIT $offset,$pagesize";
		//echo $sql;exit;
		$this->messages_db->query($sql);
		$message_data = $this->messages_db->fetch_array();
		$this->messages_db->query($sql_count);
		$total = $this->messages_db->fetch_array();
		$total = $total[0]['num'];
		$pages = pages($total, $page, $pagesize);
		$data = $this->company_data;
		include template('member', 'company_message');
	}
	//公司投标
	function buyproduct_reply(){
		$this->buyproduct_reply_db = pc_base::load_model('buy_product_reply_model');
		if($_POST['dosubmit']){
			$data = $_POST['info'];
			
			if($data['typeid']!=3 || $data['userid']==''){
				showmessage("只有商家用户才可以回复", HTTP_REFERER);
			}
			unset($data['typeid']);
			$data['content'] = addslashes($_POST['info']['content']);
			$data['addtime'] = strtotime('now');
			$result = $this->buyproduct_reply_db->add_relpy($data);
			$msg = $result==1?'回复成功':'回复失败';
			showmessage($msg, HTTP_REFERER);
		}
	}
	
}
?>